#include "stdafx.h"

#include "../Common/cTags.h"
#include "../RMSLib/utility.h"

//#include <unistd.h>
//#include <sys/time.h>
#include <sys/types.h>
//#include <sys/socket.h>


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif



int recvtimeout(int s, char *buf, int len, int timeout)
{
    fd_set fds;
    int n;
    struct timeval tv;

    // set up the file descriptor set
    FD_ZERO(&fds);
    FD_SET(s, &fds);

    // set up the struct timeval for the timeout
    tv.tv_sec = timeout;
    tv.tv_usec = 0;

    // wait until timeout or data received
    n = select(s+1, &fds, NULL, NULL, &tv);
    if (n == 0) return -2; // timeout!
    if (n == -1) return -1; // error

    // data must be here, so do a normal recv()
    return recv(s, buf, len, 0);
}

int recvfromtimeout(int s, char *buf, int max, LPSOCKADDR sa, int* len, int timeout)
{
    fd_set fds;
    int n;
    struct timeval tv;

    // set up the file descriptor set
    FD_ZERO(&fds);
    FD_SET(s, &fds);

    // set up the struct timeval for the timeout
    tv.tv_sec = timeout;
    tv.tv_usec = 0;

    // wait until timeout or data received
    n = select(s+1, &fds, NULL, NULL, &tv);
    if (n == 0) return -2; // timeout!
    if (n == -1) return -1; // error

    // data must be here, so do a normal recvfrom
    return recvfrom(s, buf, max, 0,sa,len);
}

int tcpReceiveData(SOCKET& Socket,BOOL& bTerminated, void* data, int Len)
{
	int i=0;
	while (i<Len) {
		if(bTerminated) return -1;
		int n = recv(Socket,(char*)data+i, Len-i, 0);
		if (n == SOCKET_ERROR) {
				return -1;
		}
		if (n == 0) {
 				return -1;
		}
		i+=n;
	}
	return i;
}

BOOL tcpTAGRequest(SOCKET& Socket)
{
	int n = 0;
	DWORD dwTag = TAG_GETDATA;

	if((n = send(Socket,(char*)&dwTag,4,0)) == SOCKET_ERROR)
	{
		return FALSE;
	}
	if(n != 4)
	{
		return FALSE;
	}
	return TRUE;
}


DWORD GetLocalHostIP(int No)
{
	hostent* hi;
	char name[100];
	gethostname(name,100);
	hi= gethostbyname(name);
	if (hi==0) return 0;
	if (hi->h_addr_list[No]==0) return 0;
	return  *(DWORD*)(hi->h_addr_list[No]);
}

char* GetIPStr(DWORD IP)
{
	static char name[100];
	sprintf(name,"%d.%d.%d.%d",IP&255,(IP>>8)&255,(IP>>16)&255,(IP>>24)&255);
	return name;
}


void GetDate(const char* szDate, int& nDay, int& nMonth, int& nYear)
{
	char temp[50];
	sscanf(szDate,"%s %d,%d",temp,&nDay,&nYear);
	if(strcmp(temp, "January") == 0) nMonth = 1;
	if(strcmp(temp, "February") == 0) nMonth = 2;
	if(strcmp(temp, "March") == 0) nMonth = 3;
	if(strcmp(temp, "April") == 0) nMonth = 4;
	if(strcmp(temp, "May") == 0) nMonth = 5;
	if(strcmp(temp, "June") == 0) nMonth = 6;
	if(strcmp(temp, "July") == 0) nMonth = 7;
	if(strcmp(temp, "August") == 0) nMonth = 8;
	if(strcmp(temp, "September") == 0) nMonth = 9;
	if(strcmp(temp, "October") == 0) nMonth = 10;
	if(strcmp(temp, "November") == 0) nMonth = 11;
	if(strcmp(temp, "December") == 0) nMonth = 12;
}

void GetTime(const char* szTime, int& nSecond, int& nMinute, int& nHour)
{
	sscanf(szTime,"%d:%d:%d",&nHour,&nMinute,&nSecond);
}

void GetFileName(const char* szDate, int nCameraID, CString& strFileName)
{
#ifdef _CYCLIC_BUFFER_
	int d, m, y;
	GetDate(szDate, d, m, y);

	CTime time(y, m, d, 0, 0, 0);
	strFileName.Format("%d%s",nCameraID,time.Format("%b%d%Y"));
#else
	strFileName.Format("%d",nCameraID);
	strFileName += "stored_video";
#endif
}
